package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.*;
import com.esyoga.yoyoga.pojo.DTO.MomentsDTO;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.pojo.DTO.ReviewDTO;
import com.esyoga.yoyoga.service.MomentsService;
import com.esyoga.yoyoga.service.ReviewService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Service
public class MomentsServiceImpl implements MomentsService {
    @Resource
    TMomentsMapper tMomentsMapper;
    @Resource
    TMomentsExample tMomentsExample;
    @Resource
    TStudentMapper tStudentMapper;
    @Resource
    TStudentExample tStudentExample;
    @Resource
    TTrainerExample tTrainerExample;
    @Resource
    TTrainerMapper tTrainerMapper;
    @Resource
    TAttentionExample tAttentionExample;
    @Resource
    TAttentionMapper tAttentionMapper;
    @Resource
   ReviewService reviewService;
    //所有人可见动态，前端取的时候对dto里面的学员集合和私教集合是否为空做判断，为空就不展示
    public List<MomentsDTO> findMomentsToAllUser(){
        List<MomentsDTO> momentsDTOS=new ArrayList<MomentsDTO>();
        tMomentsExample.clear();
        TMomentsExample.Criteria criteria=tMomentsExample.createCriteria();
        //查询权限为所有人可见的消息
        criteria.andMPermissionEqualTo(2);
        //0为未被删，1为以删除
        criteria.andMFlagEqualTo(0);
       List<TMoments> tMoments= tMomentsMapper.selectByExample(tMomentsExample);
       // System.out.println("所有动态"+tMoments);
       //遍历每条消息
        for (TMoments tm:tMoments
             ) {
            //new一个MomentsDTO来存放除用户外的信息
            MomentsDTO momentsDTO=new MomentsDTO();
            momentsDTO.setMId(tm.getmId());
            momentsDTO.setMUserid(tm.getmUserid());
            momentsDTO.setMUsertype(tm.getmUsertype());
            momentsDTO.setMFlag(tm.getmFlag());
            momentsDTO.setMImage(tm.getmImage());
            momentsDTO.setMLikenum(tm.getmLikenum());
            momentsDTO.setMPermission(tm.getmPermission());
            momentsDTO.setMText(tm.getmText());
            momentsDTO.setMTime(tm.getmTime());
            //根据动态id去评论表查询该动态的评论集合
           List<ReviewDTO> reviewDTOS= reviewService.findAllReviewByMid(tm.getmId());
            momentsDTO.setReviewDTOS(reviewDTOS);

            //将用户类型进行判断,0为学员，1为私教
            if(tm.getmUsertype()==0){
            //如果是学员，则去学员表查询,根据学员id查询
                tStudentExample.clear();
                TStudentExample.Criteria criteria1=tStudentExample.createCriteria();
            criteria1.andSIdEqualTo(tm.getmUserid());
            //状态为正常才能被查询到
                criteria1.andSStateEqualTo(0);
               List<TStudent> tStudents= tStudentMapper.selectByExample(tStudentExample);


                   momentsDTO.setTStudents(tStudents);



            }else {
                //用户类型为私教时
                tTrainerExample.clear();
               TTrainerExample.Criteria criteria2= tTrainerExample.createCriteria();
               criteria2.andTIdEqualTo(tm.getmUserid());
               //状态不为禁用时
               criteria2.andTStateNotEqualTo(2);
              List<TTrainer> tTrainers=  tTrainerMapper.selectByExample(tTrainerExample);
             //判断是否为空
              momentsDTO.setTTrainers(tTrainers);


            }


            momentsDTOS.add(momentsDTO);
        }
        return momentsDTOS;
    }
//查询仅好友可见,前端取的时候对dto里面的学员集合和私教集合是否为空做判断，为空就不展示
    @Override
    public List<MomentsDTO> findMomentsToPartUser(Integer userId ,Integer userType) {
        List<MomentsDTO> momentsDTOS=new ArrayList<MomentsDTO>();
        tMomentsExample.clear();
        TMomentsExample.Criteria criteria=tMomentsExample.createCriteria();
        //权限为仅限好友和所有人
        criteria.andMPermissionEqualTo(1);
        //未被删的
        criteria.andMFlagEqualTo(0);
        //获得两种权限的集合
       List<TMoments>tMoments= tMomentsMapper.selectByExample(tMomentsExample);
       // System.out.println("两种权限集合"+tMoments);
        for (TMoments tm:tMoments
        ) {
            //new一个MomentsDTO来存放除用户外的信息
            MomentsDTO momentsDTO=new MomentsDTO();

            momentsDTO.setMId(tm.getmId());
            momentsDTO.setMUserid(tm.getmUserid());
            momentsDTO.setMUsertype(tm.getmUsertype());
            momentsDTO.setMFlag(tm.getmFlag());
            momentsDTO.setMImage(tm.getmImage());
            momentsDTO.setMLikenum(tm.getmLikenum());
            momentsDTO.setMPermission(tm.getmPermission());
            momentsDTO.setMText(tm.getmText());
            momentsDTO.setMTime(tm.getmTime());
            //根据动态id去评论表查询该动态的评论集合
            List<ReviewDTO> reviewDTOS= reviewService.findAllReviewByMid(tm.getmId());
            momentsDTO.setReviewDTOS(reviewDTOS);
           //获取当前登录的人关注的人的集合,在关注表中查询
            //如果当前用户类型是学员
            if(userType==0) {
                //System.out.println("我是学员");
                tAttentionExample.clear();
                TAttentionExample.Criteria criteria1 = tAttentionExample.createCriteria();
                criteria1.andAUserid1EqualTo(userId);
                //System.out.println(userId);
                criteria1.andAUsertype1EqualTo(0);
                //查询到当前用户(学员)关注的所有人
                List<TAttention> tAttentions = tAttentionMapper.selectByExample(tAttentionExample);
                //取出此集合里所有被关注人的id
                List<Integer> userIdList = new ArrayList<Integer>();
                //循环加入
                for (TAttention tAttention : tAttentions
                ) {
                    userIdList.add(tAttention.getaUserid2());
                }
                //System.out.println("被关注者id："+userIdList);
                //将被关注人id作为条件
                tAttentionExample.clear();
                TAttentionExample.Criteria criteria2 = tAttentionExample.createCriteria();
                //查询被关注者作为主关注者在此集合里面的id和他的被关注者为当前用户的用户集合
                criteria2.andAUserid1In(userIdList);
                criteria2.andAUserid2EqualTo(userId);
                //获得相互关注的对象
                List<TAttention> tAttentionList = tAttentionMapper.selectByExample(tAttentionExample);
             // System.out.println("相互关注："+tAttentionList);
                //取出互关的被关注者id
                for (TAttention ta:tAttentionList
                     ) {
                    //System.out.println("互关的被关注者id："+ta.getaUserid1());
                    //System.out.println("互关的被关注者类型："+ta.getaUsertype1());
                    //判断被关注者是什么类型，如果是学员：
                    if(ta.getaUsertype1()==0&&tm.getmUserid()==ta.getaUserid1()){
                        tStudentExample.clear();
                        TStudentExample.Criteria criteria3=tStudentExample.createCriteria();
                        criteria3.andSIdEqualTo(ta.getaUserid1());
                        //状态为正常
                        criteria3.andSStateEqualTo(0);
                       List<TStudent> tStudents= tStudentMapper.selectByExample(tStudentExample);
                        momentsDTO.setTStudents(tStudents);
                    }else if(ta.getaUsertype1()==1&&tm.getmUserid()==ta.getaUserid1()){
                        //用户类型为私教时
                        tTrainerExample.clear();
                        TTrainerExample.Criteria criteria7= tTrainerExample.createCriteria();
                        criteria7.andTIdEqualTo(ta.getaUserid1());
                        //状态不为禁用时
                        criteria7.andTStateNotEqualTo(2);
                        List<TTrainer> tTrainers=  tTrainerMapper.selectByExample(tTrainerExample);
                        momentsDTO.setTTrainers(tTrainers);
                    }

                }
            }else {
                //System.out.println("我没有进教练");
                //用户类型为教练时
                tAttentionExample.clear();
                TAttentionExample.Criteria criteria4 = tAttentionExample.createCriteria();
                criteria4.andAUserid1EqualTo(userId);
                criteria4.andAUsertype1EqualTo(0);
                //查询到当前用户(教练)关注的所有人
                List<TAttention> tAttentions1 = tAttentionMapper.selectByExample(tAttentionExample);
                //取出此集合里所有被关注人的id
                List<Integer> userIdList1 = new ArrayList<Integer>();
                //循环加入
                for (TAttention tAttention : tAttentions1
                ) {
                    userIdList1.add(tAttention.getaUserid2());
                }
                //将被关注人id作为条件
                tAttentionExample.clear();
                TAttentionExample.Criteria criteria3 = tAttentionExample.createCriteria();
                //查询被关注者作为主关注者在此集合里面的id和他的被关注者为当前用户的用户集合
                criteria3.andAUserid1In(userIdList1);
                criteria3.andAUserid2EqualTo(userId);
                //获得相互关注的对象
                List<TAttention> tAttentionList1 = tAttentionMapper.selectByExample(tAttentionExample);
                //取出互关的被关注者id
                for (TAttention ta:tAttentionList1
                ) {
                    //判断被关注者是什么类型，如果是学员：
                    if(ta.getaUsertype1()==0&&tm.getmUserid()==ta.getaUserid1()){
                        tStudentExample.clear();
                        TStudentExample.Criteria criteria5=tStudentExample.createCriteria();
                        criteria5.andSIdEqualTo(ta.getaUserid1());
                        //状态为正常
                        criteria5.andSStateEqualTo(0);
                        List<TStudent> tStudents= tStudentMapper.selectByExample(tStudentExample);
                        momentsDTO.setTStudents(tStudents);
                    }else if(ta.getaUsertype1()==1&&tm.getmUserid()==ta.getaUserid1()){
                        //用户类型为私教时
                        tTrainerExample.clear();
                        TTrainerExample.Criteria criteria6= tTrainerExample.createCriteria();
                        criteria6.andTIdEqualTo(ta.getaUserid1());
                        //状态不为禁用时
                        criteria6.andTStateNotEqualTo(2);
                        List<TTrainer> tTrainers=  tTrainerMapper.selectByExample(tTrainerExample);
                        momentsDTO.setTTrainers(tTrainers);
                    }
                }
            }
            momentsDTOS.add(momentsDTO);
        }
        return momentsDTOS;
    }

   //根据用户id和类型查询此用户的仅自己可见的动态，前端要判断来展示学员或私教不为空的那一个
    @Override
    public Map<String, Object> findMomentsToOnlyYou(Integer userId,Integer userType) {
        Map<String,Object> result=new HashMap<String,Object>();
        //仅自己可见的动态
        tMomentsExample.clear();
       TMomentsExample.Criteria criteria= tMomentsExample.createCriteria();
       criteria.andMUseridEqualTo(userId);
       criteria.andMUsertypeEqualTo(userType);
       criteria.andMFlagEqualTo(0);
       //权限为0：仅自己可见
       criteria.andMPermissionEqualTo(0);
       //获得当前用户仅自己可见的所有动态
      List<TMoments>tMomentsList= tMomentsMapper.selectByExample(tMomentsExample);
      //将集合放入map
      result.put("tMomentsList",tMomentsList);
      //查询当前用户头像等信息
        tStudentExample.clear();
        //判断是什么类型的用户,0为学员，1为私教
        if(userType==0){
          TStudent student=  tStudentMapper.selectByPrimaryKey(userId);
          result.put("student",student);

        }else if(userType==1){
            TTrainer tTrainer=tTrainerMapper.selectByPrimaryKey(userId);
            result.put("tTrainer",tTrainer);
        }

        return result;
    }
//删除一条动态
    @Transactional
    @Override
    public String delMomentsById(Integer mId) {
        //System.out.println(mId);
       int row=0;
       String result="删除失败!";
       tMomentsExample.clear();
      TMomentsExample.Criteria criteria= tMomentsExample.createCriteria();
      criteria.andMIdEqualTo(mId);
      TMoments tMoments=new TMoments();
        tMoments.setmFlag(1);
        row= tMomentsMapper.updateByExampleSelective(tMoments,tMomentsExample);
        if(row!=0){
           result="删除成功!";
        }
        return result;
    }
//添加一条动态
    @Transactional
    @Override
    public String addOneMoments(TMoments tMoments) {
        int row=0;
        String result="发表失败!";
        TMoments tM=new TMoments();
        tM.setmUsertype(tMoments.getmUsertype());
        tM.setmUserid(tMoments.getmUserid());
        tM.setmTime(tMoments.getmTime());
        tM.setmImage(tMoments.getmImage());
        //tM.setmLikenum(tMoments.getmLikenum());
        tM.setmPermission(tMoments.getmPermission());
        tM.setmText(tMoments.getmText());
       row= tMomentsMapper.insertSelective(tM);
        if(row!=0){
            result="发表成功!";
        }
        return result;
    }
//点赞数控制
    @Transactional
    @Override
    public String addOneLikeNum(Integer currentLikenum,Integer mid,Integer likenum) {

        int row=0;
        String result="失败";
        tMomentsExample.clear();
        TMomentsExample.Criteria criteria=tMomentsExample.createCriteria();
         criteria.andMIdEqualTo(mid);

         TMoments tMoments=new TMoments();
         tMoments.setmLikenum(currentLikenum+likenum);
        row=tMomentsMapper.updateByExampleSelective(tMoments,tMomentsExample);
         if (row!=0){
             result="成功";
         }
        return result;
    }

}
